using System;
using System.Collections.Generic;
//using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Http.Results;
using System.Web.Http.OData.Query;
using LynxBlogAPI.Models;

namespace LynxBlogAPI.Controllers
{
    public class MediaObjectDataController : ApiController
    {
        private LynxWebLog_Context db = new LynxWebLog_Context();

        //// GET api/MdiaObjectData
        [Queryable]
        public IEnumerable<MediaObjectData> GetMediaObjectData()
        {
            return db.MediaObjectDatas.AsEnumerable();
        }

        //public IHttpActionResult GetMediaObjectData(ODataQueryOptions<MediaObjectData> query)
        //{
        //    IQueryable result = query.ApplyTo(db.MediaObjectDatas);
        //    return Ok(result, result.GetType());
        //}

        //private IHttpActionResult Ok(object content, Type type)
        //{
        //    Type resultType = typeof(OkNegotiatedContentResult<>).MakeGenericType(type);
        //    return Activator.CreateInstance(resultType, content, this) as IHttpActionResult;
        //}

        // GET api/MdiaObjectData/5
        [Queryable]
        public MediaObjectData GetMediaObjectData(Int32 id)
        {
            MediaObjectData mediaobjectdata = db.MediaObjectDatas.Find(id);
            if (mediaobjectdata == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return mediaobjectdata;
        }

        // PUT api/MdiaObjectData/5
        public HttpResponseMessage PutMediaObjectData(Int32 id, MediaObjectData mediaobjectdata)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != mediaobjectdata.objectid)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            db.Entry(mediaobjectdata).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }

        // POST api/MdiaObjectData
        public HttpResponseMessage PostMediaObjectData(MediaObjectData mediaobjectdata)
        {
            if (ModelState.IsValid)
            {
                db.MediaObjectDatas.Add(mediaobjectdata);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, mediaobjectdata);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = mediaobjectdata.objectid }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }

        // DELETE api/MdiaObjectData/5
        public HttpResponseMessage DeleteMediaObjectData(Int32 id)
        {
            MediaObjectData mediaobjectdata = db.MediaObjectDatas.Find(id);
            if (mediaobjectdata == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.MediaObjectDatas.Remove(mediaobjectdata);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK, mediaobjectdata);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}